package demo

import (
	"encoding/json"
	"fmt"
	"os"

	"gitee.com/general252/ebml-go"
	"gitee.com/general252/ebml-go/webm"
)

func MarshalOut(webmFile string) {
	r, err := os.Open(webmFile)
	if err != nil {
		panic(err)
	} else {
		defer r.Close()
	}

	var objectInput struct {
		Header  webm.EBMLHeader `ebml:"EBML"`
		Segment webm.Segment    `ebml:"Segment"`
	}

	if err := ebml.Unmarshal(r, &objectInput); err != nil {
		fmt.Printf("error: %v\n", err)
		return
	}

	if data, err := json.MarshalIndent(objectInput, "", "  "); err != nil {
		fmt.Printf("error: %v\n", err)
		return
	} else {
		_ = os.WriteFile(webmFile+".json", data, os.ModePerm)
	}
}
